package com.example.forum.services.impl;

import com.example.forum.entity.Message;
import com.example.forum.entity.User;
import com.example.forum.exception.CustomException;
import com.example.forum.mapper.MessageMapper;
import com.example.forum.mapper.UserMapper;
import com.example.forum.services.IMessageService;
import com.example.forum.utils.StringUtil;
import com.example.forum.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.example.forum.constant.MessageConstant.*;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 发送消息
     *
     * @param receiveUserId
     * @param content
     * @param postUserId
     */
    public void send(Long receiveUserId, String content, Long postUserId) throws CustomException {
        // 参数校验
        if (receiveUserId == null || StringUtil.isEmpty(content) || postUserId == null) {
            throw new CustomException(PARAMETER_VERIFICATION_FAILED);
        }

        // 封装消息
        Message message = new Message();
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        message.setPostUserId(postUserId);
        message.setDeleteState(0);
        message.setState(0);

        Date now = new Date();
        message.setCreateTime(now);
        message.setUpdateTime(now);

        // 发送消息并校验
        Integer send = messageMapper.send(message);
        if (send == 0) {
            throw new CustomException(MESSAGE_SEND_FAILED);
        }
    }

    /**
     * 获取所有消息
     *
     * @param userId
     * @return
     */
    public List<MessageVO> getAll(Long userId) throws CustomException {
        // 参数校验
        if (userId == null) {
            throw new CustomException(PARAMETER_VERIFICATION_FAILED);
        }

        // 获取所有消息
        List<MessageVO> messageList = messageMapper.getAll(userId);

        // 设置发送者信息
        for (MessageVO message : messageList) {
            Long postUserId = message.getPostUserId();
            User user = userMapper.getUser(postUserId);
            if (user == null) {
                throw new CustomException(SEND_NOT_EXISTS);
            }
            message.setUId(user.getId());
            message.setUAvatarUrl(user.getAvatarUrl());
            message.setUNickname(user.getNickname());
        }

        // 返回list集合
        return messageList;
    }

    /**
     * 获取未读消息数量
     *
     * @param userId
     * @return
     */
    public Integer getUnreadCount(Long userId) throws CustomException {
        // 参数校验
        if (userId == null) {
            throw new CustomException(PARAMETER_VERIFICATION_FAILED);
        }

        // 获取未读消息数量
        return messageMapper.getUnreadCount(userId);
    }

    /**
     * 标记消息已读
     *
     * @param id
     * @param userId
     */
    public void markRead(Long id, Long userId) throws CustomException {
        // 参数校验
        if (id == null || userId == null) {
            throw new CustomException(PARAMETER_VERIFICATION_FAILED);
        }

        // 获取消息, 校验消息
        Message message = messageMapper.getById(id);
        if (message == null) {
            throw new CustomException(MESSAGE_NOT_EXISTS);
        }

        // 处理消息
        Integer row = messageMapper.markRead(id, userId);

        // 校验更新结果
        if (row != 1) {
            throw new CustomException(SERVER_ERROR);
        }
    }

    /**
     * 回复消息
     *
     * @param repliedId
     * @param content
     * @param userId
     */
    @Transactional
    public void reply(Long repliedId, String content, Long userId) throws CustomException {
        // 参数校验
        if (repliedId == null || StringUtil.isEmpty(content) || userId == null) {
            throw new CustomException(PARAMETER_VERIFICATION_FAILED);
        }

        // 校验发送者
        User user = userMapper.getUser(userId);
        if (user == null) {
            throw new CustomException(USER_NOT_EXISTS);
        }

        // 获取消息, 校验消息
        Message message = messageMapper.getById(repliedId);
        if (message == null || message.getDeleteState() == 1) {
            throw new CustomException(MESSAGE_NOT_EXISTS);
        }
        // 更新消息状态为已回复
        messageMapper.replied(repliedId);

        // 封装消息
        Message repliedMessage = new Message();
        repliedMessage.setReceiveUserId(message.getPostUserId());
        repliedMessage.setContent(content);
        repliedMessage.setPostUserId(userId);
        repliedMessage.setDeleteState(0);
        repliedMessage.setState(0);

        Date now = new Date();
        repliedMessage.setCreateTime(now);
        repliedMessage.setUpdateTime(now);

        // 发送消息并校验
        Integer send = messageMapper.send(repliedMessage);
        if (send == 0) {
            throw new CustomException(MESSAGE_SEND_FAILED);
        }
    }
}
